<template>
    <div>
        <!-- 面包屑区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片主体内容 -->
        <el-card class="box-card">
            <!-- 搜索区域 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @keyup.enter.native="searchOrders"
                        @clear="searchOrders">
                        <el-button @click="searchOrders" slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
            </el-row>

            <!-- 商品列表 -->
            <el-table :data="orderList" border stripe>
                <el-table-column type="index">
                </el-table-column>

                <el-table-column prop="order_number" label="订单编号">
                </el-table-column>
                <el-table-column prop="order_price" label="订单价格（元）">
                </el-table-column>
                <el-table-column prop="pay_status" label="是否付款">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.pay_status == 1" type="success">已付款</el-tag>
                        <el-tag v-else type="danger">未付款</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="is_send" label="是否发货">
                </el-table-column>
                <el-table-column prop="create_time" label="下单时间">
                    <template slot-scope="scope">
                        {{ scope.row.create_time | dateFmt() }}
                    </template>
                </el-table-column>

                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <!-- 修改地址 -->
                        <el-button @click="editAddrVisible = true" type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <!-- 查看物流按钮 -->
                        <el-button size="mini" type="success" icon="el-icon-location"
                            @click="showTransport(scope.row.order_id)">
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 物流信息弹窗 -->
            <el-dialog :visible.sync="isTransportVisible">
                <el-timeline>
                    <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
                        {{ activity.context }}
                    </el-timeline-item>
                </el-timeline>
            </el-dialog>

            <!-- 修改收件地址 -->
            <el-dialog :visible.sync="editAddrVisible">
                <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
                    <el-form-item label="省市区/县" prop="address1">
                        <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
                    </el-form-item>
                    <el-form-item label="详细地址" prop="address2">
                        <el-input v-model="addressForm.address2"></el-input>
                    </el-form-item>
                </el-form>
            </el-dialog>

            <!-- 分页器 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 50, 100]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>

    </div>
</template>
<script>
import cityData from './citydata.js'

export default {
    data() {
        return {
            orderList: [],
            queryInfo: {
                query: '',
                pagesize: 10,
                pagenum: 1,
            },
            total: 0,
            isTransportVisible: false,
            progressInfo: [],
            addressForm: {
                address1: [], // 省市区
                address2: '' // 详细地址
            },
            addressFormRules: {
                address1: [
                    { required: true, message: '请选择省市区县', trigger: 'blur' }
                ],
                address2: [
                    { required: true, message: '请填写详细地址', trigger: 'blur' }
                ]
            },
            editAddrVisible: false,
            cityData,
        }
    },
    created() {
        this.getOrderList()
    },
    methods: {
        async getOrderList() {
            const res = await this.$http.get('orders', { params: this.queryInfo })
            console.log(res);
            if (res.status == 200) {
                this.$message.success('获取列表成功')
                this.orderList = res.data.data.goods
                this.total = res.data.total
            }
        },

        searchOrders() {
            this.pagenum = 1
            this.getOrderList()
        },

        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize;
            this.getOrderList();
        },
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage;
            this.getOrderList();
        },

        // 打开物流信息弹窗
        async showTransport(id) {
            // const res = await this.$http.get(`kuaidi/${id}`)
            const res = await this.$http.get(`kuaidi/1106975712662`)

            console.log(res);

            if (res.status !== 200) {
                this.$message.error('获取数据失败')
                return
            }

            this.progressInfo = res.data.data

            this.isTransportVisible = true
        }
    }
}
</script>
<style scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';

.el-cascader {
    width: 100%;
}
</style>